package graphics.operations;

import graphics.Cerchio;
import graphics.Forma;
import graphics.Rettangolo;
import graphics.Triangolo;
import graphics.app.GraphicsData;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;

public class SelectOperation extends Operation{

	int xStart,yStart;
	int xMove,yMove;
	
	
	public void mousePressed(MouseEvent event) {
		xStart=event.getX();
		yStart=event.getY();
	}
	
	
	public void mouseClicked(MouseEvent event) {
		//Do nothing
	}
	
	
	public void mouseDragged(MouseEvent event) {
		aggiornaFormeSelezionate(getData(), xStart,yStart,event.getX()-xStart,event.getY()-yStart);
		getData().aggiorna();
	}
	
	
	public void mouseEntered(MouseEvent event) {
		//Do nothing
	}
	
	
	public void mouseExited(MouseEvent event) {
		//Do nothing
	}
	
	
	public void mouseMoved(MouseEvent event) {
		//Do nothing
	}
	
	
	public void mouseReleased(MouseEvent event) {
		xStart=0;
		yStart=0;
		xMove=0;
		yMove=0;
		getData().aggiorna();
	}
	
	
	public void drawOperationInfo(Graphics graphics) {
		graphics.setColor(Color.RED);
		if(xMove<xStart){
			if(yMove<yStart)	
				graphics.drawRect(xMove, yMove, xStart-xMove, yStart-yMove);
			else	
				graphics.drawRect(xMove, yStart, xStart-xMove, yMove-yStart);
		}else{
			if(yMove<yStart)
				graphics.drawRect(xStart, yMove, xMove-xStart, yStart-yMove);
			else
				graphics.drawRect(xStart, yStart, xMove-xStart, yMove-yStart);
		}
		for (int i = 0; i < getData().getSelezioneFormeCount(); i++) {
			getData().getSelezioneForma(i).draw(graphics);
		}
		graphics.setColor(Color.BLACK);
	}
	
	
	public void aggiornaFormeSelezionate(GraphicsData data,int x,int y,int w,int h){
		
		xMove=x+w;
		yMove=y+h;
		
		if(w<0){
			w=-w;
			x=x-w;
		}
		if(h<0){
			h=-h;
			y=y-h;
		}
		
		data.pulisciSelezione();
		
		for (int i = 0; i < data.getFormeCount(); i++) {
			Forma forma=data.getForma(i);
			
			if(forma instanceof Cerchio) {
				Cerchio cerchio=(Cerchio)forma;
				int xp=cerchio.getX();
				int yp=cerchio.getY();
				int r=cerchio.getR();
				if(xp-r>=x && xp+r<=x+w){
					if(yp-r>=y && yp+r<=y+h){
						data.addSelezioneForma(cerchio);
					}
				}
			}
			
			if(forma instanceof Rettangolo) {
				Rettangolo rettangolo=(Rettangolo)forma;
				int xp=rettangolo.getX();
				int yp=rettangolo.getY();
				int W=rettangolo.getW();
				int H=rettangolo.getH();
				if(xp>=x && xp+W<=x+w){
					if(yp>=y && yp+H<=y+h){
						data.addSelezioneForma(rettangolo);
					}
				}
			}
			
			if(forma instanceof Triangolo) {
				Triangolo triangolo=(Triangolo)forma;
				if(staPuntoInRettangolo(triangolo.getX1(),triangolo.getY1(),x,y,w,h)){
					if(staPuntoInRettangolo(triangolo.getX2(),triangolo.getY2(),x,y,w,h)){
						if(staPuntoInRettangolo(triangolo.getX3(),triangolo.getY3(),x,y,w,h)){
							data.addSelezioneForma(triangolo);
						}
					}
				}
			}
			
		}
	}
	
	private boolean staPuntoInRettangolo(int xp,int yp,int x,int y,int w,int h){
		if(xp>=x && xp<=x+w){
			if(yp>=y && yp<=y+h){
				return true;
			}
		}
		return false;
	}
}
